#include "Inventory.h"

Inventory::Inventory(void)
{
	this->row = 0;
	this->column = 0;
	this->draw = false;

	LoadTGA(&IconTexture,	"images/InventoryFrame.tga");

	ItemTexture = new TextureImage[3];
	LoadTGA(&ItemTexture[0],	"images/FrameBag.tga");
	LoadTGA(&ItemTexture[1],	"images/FrameBrokenBow.tga");
	LoadTGA(&ItemTexture[2],	"images/FrameBrokenSword.tga");

	inventory_font.init("04B_03__.ttf", 8);
	quick_font.init("Test.ttf", 15);
}

Inventory::Inventory(int row, int column)
{
	slots = new Item*[row];

	for(int i = 0 ; i < row; i++)
	{
		slots[i] = new Item[column];

		for(int j = 0; j < column; j++)
		{
			slots[i][j].SetItemNo(Item::no::NOTHING);
			slots[i][j].SetItemType(Item::type::EMPTY);
		}
	}

	this->row = row;
	this->column = column;
	this->draw = false;

	LoadTGA(&IconTexture,	"images/InventoryFrame.tga");

	ItemTexture = new TextureImage[4];
	LoadTGA(&ItemTexture[0],	"images/Lobby/Potion.tga");
	LoadTGA(&ItemTexture[1],	"images/FrameBag.tga");
	LoadTGA(&ItemTexture[2],	"images/FrameBrokenBow.tga");
	LoadTGA(&ItemTexture[3],	"images/FrameBrokenSword.tga");

	inventory_font.init("04B_03__.ttf", 8);
	quick_font.init("Test.ttf", 15);
}

Inventory::~Inventory(void)
{
	for(int i = 0; i < row ; i++)
		delete [] slots[i];

	delete [] slots;

	delete [] ItemTexture;
}

void Inventory::Print(void)
{
	for(int i = 0 ; i < row; i++)
	{
		for(int j = 0; j < column; j++)
		{
			cout << slots[i][j].ReturnItemNo() << " , ";
		}
		cout << endl;
	}
}

void Inventory::SetMouseInfo(MouseInfo *mouseInfo)
{
	this->mouseInfo =  mouseInfo;
}

bool Inventory::GetDraw(void)
{
	return this->draw;
}

void Inventory::SetDraw(bool draw)
{
	this->draw = draw;
}

void Inventory::UpdateInventory(void)
{
	
}

void Inventory::RenderInventory(void)
{
	//Variable Used for mousing over icon
	bool MousedOverIcon = false;

	glPushAttrib(GL_CURRENT_BIT);
	//glEnable(GL_TEXTURE_2D);
	glEnable(GL_BLEND);
		//stuffs to draw when only player open inventory
		if(this->draw)
		{
			//Background for Inventory
			glPushMatrix();
			glColor4f(0.3f, 0.3f, 0.3f, 0.3f);
			glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
			glBegin(GL_QUADS);

			glVertex2f(0, 0);

			glVertex2f(WIDTH, 0);

			glVertex2f(WIDTH, HEIGHT);

			glVertex2f(0, HEIGHT);

			glEnd();
			glPopMatrix();

			//Background Icons for inventory
			for(int i = 0; i < row ; i++)
			{
				for(int j = 0; j< column; j++)
				{
					glEnable(GL_TEXTURE_2D);
					glPushMatrix();
						glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
					glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
					glBindTexture(GL_TEXTURE_2D, IconTexture.texID);
					glBegin(GL_QUADS);

					glTexCoord2f(0,1);glVertex2f(j * 2 * 50 + 125, 150 + i * 2 * 32.5f);

					glTexCoord2f(0,0);glVertex2f(j * 2 * 50 + 125 + INVSIZE, 150 + i * 2 * 32.5f);

					glTexCoord2f(1,0);glVertex2f(j * 2 * 50 + 125 + INVSIZE, 150 + INVSIZE + i * 2 * 32.5f);

					glTexCoord2f(1,1);glVertex2f(j * 2 * 50 + 125, 150 + INVSIZE + i * 2 * 32.5f);

					glEnd();
					glPopMatrix();

					//Rendering of items on inventory
					glPushMatrix();
					if(slots[i][j].ReturnItemNo() == Item::no::POTIONS)
						glBindTexture(GL_TEXTURE_2D, ItemTexture[0].texID);
					else if(slots[i][j].ReturnItemNo() == Item::no::LEATHER)
						glBindTexture(GL_TEXTURE_2D, ItemTexture[1].texID);
					else if(slots[i][j].ReturnItemNo() == Item::no::SWORDPIECE)
						glBindTexture(GL_TEXTURE_2D, ItemTexture[2].texID);
					else if(slots[i][j].ReturnItemNo() == Item::no::ARROWHEAD)
						glBindTexture(GL_TEXTURE_2D, ItemTexture[3].texID);
					else
						glColor4f(1.0f, 1.0f, 1.0f, 0.0f);
					glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
					
					//If mouse is over an icon set to true else false;
					//if(mouseInfo->lastX <= j * 2 * 50 + 125 + INVSIZE && mouseInfo->lastX >= j * 2 * 50 + 125 &&
					//	mouseInfo->lastY <= 150 + INVSIZE + i * 2 * 32.5f && mouseInfo->lastY >= 150 + i * 2 * 32.5f)
					//	MousedOverIcon = true;
					//else
					//	MousedOverIcon = false;

					////if mouse is over icon and left mouse button is hold down move the icone according to mouse position
					//if(MousedOverIcon)
					//{
					//	glTranslatef(mouseInfo->lastX,mouseInfo->lastY,0);
					//	glBegin(GL_QUADS);

					//	glVertex2f(mouseInfo->lastX, mouseInfo->lastY);

					//	glVertex2f(mouseInfo->lastX + INVSIZE, mouseInfo->lastY);

					//	glVertex2f(mouseInfo->lastX + INVSIZE, INVSIZE + mouseInfo->lastY);

					//	glVertex2f(mouseInfo->lastX, INVSIZE + mouseInfo->lastY);

					//	glEnd();
					//}
					//else
					{
						glBegin(GL_QUADS);

						glTexCoord2f(0,1);glVertex2f(j * 2 * 50 + 125, 150 + i * 2 * 32.5f);

						glTexCoord2f(0,0);glVertex2f(j * 2 * 50 + 125 + INVSIZE, 150 + i * 2 * 32.5f);

						glTexCoord2f(1,0);glVertex2f(j * 2 * 50 + 125 + INVSIZE, 150 + INVSIZE + i * 2 * 32.5f);

						glTexCoord2f(1,1);glVertex2f(j * 2 * 50 + 125, 150 + INVSIZE + i * 2 * 32.5f);

						glEnd();
					}
					glPopMatrix();
					glDisable (GL_TEXTURE_2D);
				}
			}

			//If moused over items and right click it will show desciptions 
			if(!mouseInfo->mRButtonUp)
			{
				for(int i = 0; i < row ; i++)
				{
					for(int j = 0; j< column; j++)
					{
						if(mouseInfo->lastX <= j * 2 * 50 + 125 + INVSIZE && mouseInfo->lastX >= j * 2 * 50 + 125 &&
							mouseInfo->lastY <= 150 + INVSIZE + i * 2 * 32.5f && mouseInfo->lastY >= 150 + i * 2 * 32.5f
							&& mouseInfo->mLButtonUp && slots[i][j].ReturnItemNo() != Item::no::NOTHING)
						{
							glPushMatrix();
							glColor4f(0.5f, 0.5f, 0.5f, 1.0f);
							//Translate by mousesize so to not block text
							glTranslatef(11,13,0);
							glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
							glBegin(GL_QUADS);

							glVertex2f(mouseInfo->lastX, mouseInfo->lastY);

							glVertex2f(mouseInfo->lastX + 125, mouseInfo->lastY );

							glVertex2f(mouseInfo->lastX + 125, mouseInfo->lastY + 100);

							glVertex2f(mouseInfo->lastX , mouseInfo->lastY + 100);

							glEnd();
							glPopMatrix();

							glColor3f(1.0f, 1.0f, 1.0f);
							freetype::print(inventory_font, mouseInfo->lastX + 16, -mouseInfo->lastY + (HEIGHT) - 23, "Name : %s", slots[i][j].ReturnItemName().c_str());
							switch(slots[i][j].ReturnItemType())
							{
							case 1:
								freetype::print(inventory_font, mouseInfo->lastX + 16, -mouseInfo->lastY + (HEIGHT) - 33, "Type : Consumables");
								break;

							case 2:
								freetype::print(inventory_font, mouseInfo->lastX + 16, -mouseInfo->lastY + (HEIGHT) - 33, "Type : Materials");
								break;

							case 3:
								freetype::print(inventory_font, mouseInfo->lastX + 16, -mouseInfo->lastY + (HEIGHT) - 33, "Type : Unique");
								break;

							case 4:
								freetype::print(inventory_font, mouseInfo->lastX + 16, -mouseInfo->lastY + (HEIGHT) - 33, "Type : Quest");
								break;

							default:
								freetype::print(inventory_font, mouseInfo->lastX + 16, -mouseInfo->lastY + (HEIGHT) - 33, "Type : Unknown");
								break;
							}
							freetype::print(inventory_font, mouseInfo->lastX + 16, -mouseInfo->lastY + (HEIGHT) - 43, "%s", slots[i][j].ReturnItemDescription().c_str());
							freetype::print(inventory_font, mouseInfo->lastX + 16, -mouseInfo->lastY + (HEIGHT) - 53, "%s", slots[i][j].ReturnItemDescription2().c_str());
						}
					}
				}
			}
		}

		//Icon for quick slots
		for(int i = 0; i < 2; i+=2)
		{
			glEnable(GL_TEXTURE_2D);
			glPushMatrix();
			glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
			glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
			//Potions
			glBindTexture(GL_TEXTURE_2D, ItemTexture[0].texID);
			glBegin(GL_QUADS);

			glTexCoord2f(0,1);glVertex2f(i * 40 + 40, 40);

			glTexCoord2f(1,1);glVertex2f(i * 40 + 40 + QUICKSLOT, 40);

			glTexCoord2f(1,0);glVertex2f(i * 40 + 40 + QUICKSLOT,  40 + QUICKSLOT);

			glTexCoord2f(0,0);glVertex2f(i * 40 + 40, 40 + QUICKSLOT);

			glEnd();
			glPopMatrix();
			glDisable (GL_TEXTURE_2D);
		}
		
		freetype::print(quick_font, 65, HEIGHT - 80, "%i", CheckQuantity(Item::no::POTIONS) );

	glDisable(GL_BLEND);
	//glDisable(GL_TEXTURE_2D);
	glPopAttrib();
}
void Inventory::SetInventory(int X, int Y, Item item)
{
	if(X >= row || Y >= column)
		return;
	else
		slots[X][Y] = item;
}

bool Inventory::AddItem(Item item)
{
	for(int i = 0; i < row; i++)
	{
		for(int j = 0; j < column; j++)
		{
			if(slots[i][j].ReturnItemType() == Item::type::EMPTY && slots[i][j].ReturnItemNo() == Item::no::NOTHING)
			{
				slots[i][j] = item;
				AchAddItemGetCount(1);
				return true;
			}
		}
	}
	
	return false;
}

bool Inventory::CheckItem(Item::no itemNum,int quantity)
{
	for(int i = 0; i < row; i++)
	{
		for(int j = 0; j < column; j++)
		{
			if(slots[i][j].ReturnItemNo() == itemNum)
				quantity--;
			if(!quantity)
				return true;
		}
	}
	return false;
}

void Inventory::RemoveItem(Item::no itemNum, int quantity)
{
	int temp = 0;
	for(int i = 0; i < row; i++)
	{
		for(int j = 0; j < column; j++)
		{
			if(slots[i][j].ReturnItemNo() == itemNum)
			{
				slots[i][j].SetItemNull();
				temp ++;
			}
			if(temp >= quantity)
				return;
		}
	}
}

int Inventory::ReturnRow(void)
{
	return this->row;
}

int Inventory::ReturnColumn(void)
{
	return this->column;
}

int Inventory::ReturnInventory(int row, int column)
{
	if(row < this->row && column < this->column)
		return slots[row][column].ReturnItemNo();
	else
		return 0;
}

void Inventory::RemoveQuestItem(void)
{
	for(int i = 0; i < row; i++)
	{
		for(int j = 0; j < column; j++)
		{
			if(slots[i][j].ReturnItemType() == Item::type::QUEST)
				slots[i][j].SetItemNull();
		}
	}
}

void Inventory::ResetInventory(void)
{
	for(int i = 0; i < row; i++)
	{
		for(int j = 0; j < column; j++)
		{
				slots[i][j].SetItemNull();
		}
	}
}

int Inventory::CheckQuantity(Item::no itemNum)
{
	int temp = 0;
	for(int i = 0; i < row; i++)
	{
		for(int j = 0; j < column; j++)
		{
			if(slots[i][j].ReturnItemNo() == itemNum)
				temp++;
		}
	}
	return temp;
}